/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.bo;

import com.ecwid.mailchimp.MailChimpClient;
import com.ecwid.mailchimp.MailChimpException;
import com.ecwid.mailchimp.MailChimpMethod;
import com.ecwid.mailchimp.method.list.ListSubscribeMethod;
import com.ecwid.mailchimp.method.list.ListUnsubscribeMethod;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class MailChimpClientCRUD {

    private static Logger _logger = Logger.getLogger(MailChimpClientCRUD.class.getName());
    private static MailChimpClientCRUD _singleton;
    private MailChimpClient _mailChimpClient;
    private static final ResourceBundle rscBundle = ResourceBundle.getBundle("application");
    private static final String _apikey = rscBundle.getString("apikey");
    private static final String _id = rscBundle.getString("id");

    private MailChimpClientCRUD() {
        _mailChimpClient = new MailChimpClient();
        
    }

    public static MailChimpClientCRUD getInstance() {
        if (_singleton == null) {
            _singleton = new MailChimpClientCRUD();
        }
        return _singleton;
    }

    public ListSubscribeMethod getListSubscribeMethod() {
        ListSubscribeMethod listSubscribeMethod = new ListSubscribeMethod();
        listSubscribeMethod.apikey = _apikey;
        listSubscribeMethod.id = _id;
        listSubscribeMethod.double_optin = false;
        listSubscribeMethod.update_existing = true;
        return listSubscribeMethod;
    }

    public ListUnsubscribeMethod getListUnsubscribeMethod() {
        ListUnsubscribeMethod listUnsubscribeMethod = new ListUnsubscribeMethod();
        listUnsubscribeMethod.apikey = _apikey;
        listUnsubscribeMethod.id =_id;
        listUnsubscribeMethod.delete_member = false;
        return listUnsubscribeMethod;
    }
    
    
    public void execute(MailChimpMethod method){
        try {
            _mailChimpClient.execute(method);
        } catch (IOException ex) {
            _logger.error("IOException ", ex);
        } catch (MailChimpException ex) {
            _logger.error("MailChimpException ", ex);
        }
    }
    
}
